﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;
using System.Data.Entity;
namespace Benefit.Service.View
{
    /// <summary>
    /// 操作员日交易信息
    /// </summary>
    public class ServiceOperatorDayChange : Benefit.Interface.View.IOperatorDayChange
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorDayChange(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 查询交易员交易基础数据(这个方法应该没问题)
        /// </summary>
        /// <returns></returns>
        public void InitOperatorDayChange(int tradeHistoryId)
        {

            Models.Sys.TradeHistory history = db.TradeHistorys.Find(tradeHistoryId);

            Interface.Sys.IOperator iop = new Sys.ServiceOperator(db);
            var querya = iop.GetUnDeletedOperators();


            foreach (Models.Sys.Operator op in querya)
            {
                OperatorDayChange change = new OperatorDayChange();
                Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
                Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
                List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetPdateFromTradeHistoryId(tradeHistoryId), op.Id);
                change.OperatorId = op.Id;
                change.TradeHistoryId = tradeHistoryId;
                if (accounts.Count > 0)
                {
                    foreach (Models.Sys.Account account in accounts)
                    {
                        var query = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query.Count() > 0)
                        {
                            change.BillCount += query.First().BillCount;
                            change.DayCount += query.First().DayCount;
                            change.Free += query.First().Free;
                            change.LoseCount += query.First().LoseCount;
                            change.OperatorId = op.Id;
                            change.Profit += query.First().Profit;
                            change.SumLost += query.First().SumLost;
                            change.SumWin += query.First().SumWin;
                            change.TradeHistoryId = tradeHistoryId;
                            change.UsedMargin += query.First().UsedMargin;
                            change.WinCount += query.First().WinCount;
                        }
                        var _query = from t in db.AccountDayAnalysis where t.AccountId == account.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (_query.Count() > 0)
                        {
                            change.AvgBillBenefit += _query.First().AvgBillBenefit;
                            change.AvgPostionTime += _query.First().AvgPostionTime;
                            //if (_query.First().MaxReturn > change.MaxReturn)
                            //{
                            change.MaxReturn = _query.First().MaxReturn;
                            //}
                            change.OverStopLoss += _query.First().OverStopLoss;
                            change.PingJinPing += _query.First().PingJinPing;
                            change.RaiseRate += _query.First().RaiseRate;
                            change.RiskCount += _query.First().RiskCount;
                            change.Utilization += _query.First().Utilization;
                            change.WinRate += _query.First().WinRate;
                            change.Yield += _query.First().Yield;
                        }
                    }
                    change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / accounts.Count, 2);
                    //   change.AvgBillBenefit.ToString().Debug();
                    change.AvgPostionTime = Math.Round(change.AvgPostionTime / accounts.Count, 2);
                    //   change.AvgPostionTime.ToString().Debug();
                    change.Utilization = Math.Round(change.Utilization / accounts.Count, 2);
                    //    change.Utilization.ToString().Debug();
                    change.WinRate = Math.Round(change.WinRate / accounts.Count, 2);
                    //  change.WinRate.ToString().Debug();
                }
                db.OperatorDayChange.Add(change);
            }
            db.SaveChanges();
        }



        /// <summary>
        /// 查询交易员交易基础数据(这个方法应该没问题)
        /// </summary>
        /// <returns></returns>
        public List<OperatorDayChange> GetOperatorDayChanges(int tradeHistoryId)
        {
            var odc = db.OperatorDayChange.Where(a => a.TradeHistoryId == tradeHistoryId);
            return odc.ToList();
        }


        public OperatorDayChange GetOperatorDayChange(int operatorId, int tradeHistoryId)
        {
            var query = db.OperatorDayChange.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistoryId == tradeHistoryId).Include(a => a.Operator);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new OperatorDayChange();
            }
        }

        public List<OperatorDayChange> GetOperatorDayChange(int operatorId, int month, int year)
        {
            var query = db.OperatorDayChange.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistory.MPdate == month).Where(a => a.TradeHistory.YPdate == year).Include(a => a.Operator);

            return query.ToList();

        }

        public void Delete(int tradeHistoryId)
        {
            var query = db.OperatorDayChange.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (OperatorDayChange c in query)
            {
                db.OperatorDayChange.Remove(c);
            }
            db.SaveChanges();
        }
    }
}
